package com.workoutgrabber.ui.panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import org.netbeans.validation.api.builtin.Validators;
import org.netbeans.validation.api.ui.ValidationGroup;
import org.netbeans.validation.api.ui.ValidationPanel;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.toedter.calendar.JDateChooser;
import com.workoutgrabber.ui.listeners.ExerciseCreatedEvent;
import com.workoutgrabber.ui.listeners.ExerciseCreatedListener;
import com.workoutgrabber.ui.model.ExerciseTableModel;

public class WorkoutPanel extends JPanel implements ActionListener, ExerciseCreatedListener {
	public static final String COLUMN_DEFINITION = "right:max(50dlu;p), 4dlu, 75dlu, 7dlu, right:max(30dlu;p), 4dlu, 75dlu";
	
	private JTextField textFieldWorkoutName;
	private JDateChooser startDate, endDate;
	private JButton buttonSave, buttonTransfer; 
	private ExerciseTableModel exerciseTableModel;
	private ValidationPanel validationPanel;
	private ValidationGroup validationGroup;
	private ExerciseChooser exerciseChooser;
	private JComboBox comboBoxExerciseChooser;
	
	public WorkoutPanel(ExerciseTableModel exerciseTableModel, ExerciseChooser exerciseChooser){
		this.exerciseTableModel = exerciseTableModel; 
		
		this.exerciseChooser = exerciseChooser; 
		exerciseChooser.addExerciseCreateListener(this);
		
		buildPanel();
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == buttonSave)
			JOptionPane.showMessageDialog(this, "You pressed save!");
		
		if(e.getSource() == buttonTransfer)
			JOptionPane.showMessageDialog(this, "You pressed transfer!");
		
		if(e.getSource() == comboBoxExerciseChooser)
			exerciseChooser.showPanel(ExerciseName.getValueOf(comboBoxExerciseChooser.getSelectedItem().toString()));
	}	

	@Override
	public void exerciseCreated(ExerciseCreatedEvent event) {
		System.out.println(event.getExercise().getAsXML());
	}
	
//--------------------
// private methods
//--------------------
	
	private void buildPanel() {
		this.setLayout(new BorderLayout());
		this.add(buildWorkoutPanel(), BorderLayout.CENTER);
		this.add(buildControlPanel(), BorderLayout.SOUTH);
	}
	
	private JPanel buildWorkoutPanel(){				
		String columnDefinition = "p";
		
		StringBuilder rowDefinition = new StringBuilder();
		rowDefinition.append("p,"); 	// Workout details
		rowDefinition.append("10dlu,");	// Cap
		rowDefinition.append("p,"); 	// Exercise panel
		rowDefinition.append("0dlu,"); 	// Cap
		rowDefinition.append("p,"); 	// Exercise table
		rowDefinition.append("3dlu,"); 	// Cap
		rowDefinition.append("p"); 		// Control panel		
				
		PanelBuilder builder = new PanelBuilder(new FormLayout(columnDefinition, rowDefinition.toString()));
		builder.setDefaultDialogBorder();
		CellConstraints cc = new CellConstraints();
		
		builder.add(buildWorkoutDetailsSection(), cc.xy(1, 1));
		builder.add(exerciseChooser, cc.xy(1, 3));
		buildExerciseTableSection(builder, cc);
		
		return builder.getPanel();		
	}

	private JPanel buildWorkoutDetailsSection() {		
		initCompoenets();
		initValidation();
		
		String rowDefinition = "p, 5dlu, p, 3dlu, p, 5dlu, p";		
		FormLayout layout = new FormLayout(COLUMN_DEFINITION, rowDefinition.toString());

		PanelBuilder builder = new PanelBuilder(layout);
		CellConstraints cc = new CellConstraints();
		
		builder.addSeparator("Workout details", cc.xyw(1, 1, 7));
		
		builder.addLabel("Name", cc.xy (1, 3));
		builder.add(textFieldWorkoutName, cc.xy (3, 3));
		builder.addLabel("start date", cc.xy (5, 3));
		builder.add(startDate, cc.xy (7, 3));

		builder.addLabel("Choose exercise", cc.xy(1, 5));
		builder.add(comboBoxExerciseChooser, cc.xy(3, 5));
		builder.addLabel("end date", cc.xy (5, 5));
		builder.add(endDate, cc.xy (7, 5));
			
		builder.add(validationPanel, cc.xyw(1, 7, 7));
		
		return builder.getPanel();
	}
	
	private void initCompoenets() {
		textFieldWorkoutName = new JTextField();
		textFieldWorkoutName.setName("Name");
		
		comboBoxExerciseChooser = new JComboBox(ExerciseName.getAllNames());
		comboBoxExerciseChooser.addActionListener(this);
		
		startDate = new JDateChooser();
		endDate	= new JDateChooser();
	}

	private void initValidation() {
		validationPanel = new ValidationPanel();		
		validationGroup = validationPanel.getValidationGroup();
		validationGroup.add(textFieldWorkoutName, Validators.REQUIRE_NON_EMPTY_STRING);
	}

	private void buildExerciseTableSection(PanelBuilder builder, CellConstraints cc) {
		JTable tableOfExercises = new JTable(exerciseTableModel);
		JScrollPane scrollTable = new JScrollPane(tableOfExercises);
		
		scrollTable.setPreferredSize(new Dimension(this.getWidth(), 100));
		scrollTable.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		tableOfExercises.setFillsViewportHeight(true);

		builder.addSeparator("Added exercises", cc.xy(1, 5));
		builder.add(scrollTable, cc.xy(1, 7));
	}
	
	private JPanel buildControlPanel(){
		JPanel controlPanel = new JPanel();
		controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.X_AXIS));
	
		buttonSave = new JButton("Save");
		buttonSave.addActionListener(this);
		buttonSave.setAlignmentX(JButton.RIGHT_ALIGNMENT);

		buttonTransfer = new JButton("Transfer");
		buttonTransfer.addActionListener(this);
		buttonTransfer.setAlignmentX(JButton.RIGHT_ALIGNMENT);		
		
		controlPanel.add(Box.createGlue());
		controlPanel.add(buttonSave);
		controlPanel.add(Box.createRigidArea(new Dimension(10, 0)));
		controlPanel.add(buttonTransfer);
		controlPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		
		return controlPanel;
	}
}
